package com.inspur.gs.sbssdl.sbyxgl.djjh2.djjh2.front.djpzxz_frm.voactions;
import com.inspur.edp.bef.api.lcp.ILcpFactory;
import com.inspur.edp.bef.api.lcp.IStandardLcp;
import com.inspur.edp.bef.api.services.IBefSessionManager;
import com.inspur.edp.bff.spi.*;
import com.inspur.edp.bef.api.action.*;
import com.inspur.edp.cef.entity.changeset.ModifyChangeDetail;
import com.inspur.edp.cef.entity.entity.IEntityData;
import io.iec.edp.caf.commons.utils.SpringBeanUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class xzdatasVOAction extends AbstractFSAction<VoidActionResult> {

	private String[] ids;
	private Object[] data;
	public xzdatasVOAction(String[] ids, Object[] data) {
		this.ids = ids;
		this.data = data;
	}
	@Override
	public void execute() {
		System.out.println(ids);
		List<djpzRecord> djpzlist = new ArrayList<>();
		for (Object datum : data) {
			djpzRecord record = parseRecord(datum.toString());
			djpzlist.add(record);
			System.out.println(datum.toString());
		}
		SpringBeanUtils.getBean(IBefSessionManager.class).createSession();
		try {
			//点检配置变更
			ILcpFactory iLcpFactory = SpringBeanUtils.getBean(ILcpFactory.class);
			String djid = "ff554cce-1197-469a-88f1-814f2ed0781d";// 设备点检配置id
			IStandardLcp djlcp = iLcpFactory.createLcpByBEId(djid);

			for (String id : ids) {
				// 主表变更
				ModifyChangeDetail change = new ModifyChangeDetail(id);
				change.getPropertyChanges().put("djpz",true);
				djlcp.modify(change);
				djlcp.save();

				//明细变更
				String djxzid = "d63f2f8e-2c5d-441d-9ba7-1edc3712159c";// 设备点检配置明细id
				IStandardLcp djxzlcp = iLcpFactory.createLcpByBEId(djxzid);

				for (djpzRecord item : djpzlist) {
					IEntityData newData=djxzlcp.retrieveDefault();
					ModifyChangeDetail curChange = new ModifyChangeDetail(newData.getID());
					curChange.getPropertyChanges().put("ParentID",id);
					curChange.getPropertyChanges().put("djjg",item.getDjjg());
					curChange.getPropertyChanges().put("djbw",item.getDjbw());
					curChange.getPropertyChanges().put("djnr",item.getDjnr());
					curChange.getPropertyChanges().put("bwnr",item.getBwsm());
					curChange.getPropertyChanges().put("zysx",item.getZysx());
					djxzlcp.modify(curChange);
				}
			djxzlcp.save();
			}
		} finally {
			SpringBeanUtils.getBean(IBefSessionManager.class).closeCurrentSession();
		}

	}

	public static class djpzRecord {
		private String sbdl;
		private String sbxl;
		private String djjg;
		private String djbw;
		private String djnr;
		private String bwsm;
		private String zysx;

		public String getSbdl() {
			return sbdl;
		}

		public void setSbdl(String sbdl) {
			this.sbdl = sbdl;
		}

		public String getSbxl() {
			return sbxl;
		}

		public void setSbxl(String sbxl) {
			this.sbxl = sbxl;
		}

		public String getDjjg() {
			return djjg;
		}

		public void setDjjg(String djjg) {
			this.djjg = djjg;
		}

		public String getDjbw() {
			return djbw;
		}

		public void setDjbw(String djbw) {
			this.djbw = djbw;
		}

		public String getDjnr() {
			return djnr;
		}

		public void setDjnr(String djnr) {
			this.djnr = djnr;
		}

		public String getBwsm() {
			return bwsm;
		}

		public void setBwsm(String bwsm) {
			this.bwsm = bwsm;
		}

		public String getZysx() {
			return zysx;
		}

		public void setZysx(String zysx) {
			this.zysx = zysx;
		}

	}

	public static djpzRecord parseRecord(String recordStr) {
		String content = recordStr.substring(1, recordStr.length() - 1);
		String[] pairs = content.split(", ");

		Map<String, String> map = new HashMap<>();
		for (String pair : pairs) {
			String[] kv = pair.split("=", 2);
			if (kv.length == 2) {
				map.put(kv[0], kv[1].equals("null") ? null : kv[1]);
			}
		}

		djpzRecord record = new djpzRecord();
		try {
			record.setSbdl(map.get("sbdl"));
			record.setSbxl(map.get("sbxl"));
			record.setDjjg(map.get("djjg"));
			record.setDjbw(map.get("djbw"));
			record.setDjnr(map.get("djnr"));
			record.setBwsm(map.get("bwsm"));
			record.setZysx(map.get("zysx"));
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
		return record;
	}
}
